package com.apes.scm.masterdata.car.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.scm.masterdata.car.model.CarConfiguration;
import com.apes.scm.masterdata.car.model.CarModelServiceLevel;
import com.apes.scm.masterdata.car.model.CarServiceLevel;
import com.apes.scm.masterdata.car.repository.CarConfigurationRepository;
import com.apes.scm.masterdata.car.repository.CarModelServiceLevelRepository;
import com.apes.scm.masterdata.car.repository.CarServiceLevelRepository;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 功能：
 *
 * @Auther: linwz
 * @Date: 2018/7/25 09:42
 */
@Service("carServiceLevelService")
public class CarServiceLevelService extends DomainService {

    @Autowired
    CarServiceLevelRepository carServiceLevelRepository;

    public CarServiceLevel save(SimpleRequest request) {
        CarServiceLevel carServiceLevel = request.getO(CarServiceLevel.class);
        return carServiceLevelRepository.saveAndFlush(carServiceLevel);
    }

    public String delete(SimpleRequest request) {
        String id = request.get("id");
        Integer versions = request.get("version");
        CarServiceLevel carServiceLevel = request.getO(CarServiceLevel.class);
        if (carServiceLevel == null) throw new RuntimeException("汽车服务等级【"+ id +"】在系统中不存在。");
        if (carServiceLevel.getVersion() != versions) throw new RuntimeException("记录已更新或已删除，请重新提交！");
        carServiceLevelRepository.delete(carServiceLevel);
        // Sort sort = new Sort(Sort.Direction.DESC, "id");
        // return carServiceLevelRepository.findAll( PageRequest.of(0, 20, sort));
        return "删除成功";
    }


    /**
     * 功能：查询
     *
     * @param request
     */
    public Page<CarServiceLevel> findAll(SimpleRequest request) {
        List<Map> filters = request.get("condition");
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        return carServiceLevelRepository.findAll(JpaDsl.toCriteria(filters), PageRequest.of(request.get("page"), request.get("size"), sort));
    }

    /**
     * 功能：查询
     *
     * @param request
     */
    public Page<CarServiceLevel> find(SimpleRequest request) {
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        return carServiceLevelRepository.findAll(PageRequest.of(request.get("page"), request.get("size"), sort));
    }

    /**
     * 根据配置车型，查找服务等级
     * @param jekunModelCode
     * @return
     */
    public CarServiceLevel findByCarConfigurationId(String jekunModelCode){ //2021年02-25 若根据传入jekun车型找不到对应的服务等级的时候，默认返回B级 ESB-5726
        if(jekunModelCode == null) throw new RuntimeException("无车型服务等级");
        CarConfiguration carConfiguration = SpringManager.getBean(CarConfigurationRepository.class).findOne(jekunModelCode);
        if(carConfiguration == null || carConfiguration.getCarModel() == null) return rTorExcept(SpringManager.getBean(CarServiceLevelRepository.class).findOne("20"),"主数据未定义B级服务等级");
        List<CarModelServiceLevel> carModelServiceLevels = SpringManager.getBean(CarModelServiceLevelRepository.class).findAll(JpaDsl.toCriteriaByEq("carModel.id",carConfiguration.getCarModel().getId()));
        if(carModelServiceLevels.size() == 0) return rTorExcept(SpringManager.getBean(CarServiceLevelRepository.class).findOne("20"),"主数据未定义B级服务等级");
        return carModelServiceLevels.stream().findFirst().get().getCarServiceLevel();
    }

    /**
     * 查询得到结果返回，查询不到报错
     * @param t
     * @param message
     * @param <T>
     * @return
     */
    private  <T>T rTorExcept(T t,String message){
        if(t == null) throw new RuntimeException(message);
        return t;
    }
}
